package com.wuhenzhizao.sku.utils;

import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Optional;

/**
 * 屏幕工具类
 *
 * @author wuhenzhizao
 * @since 2017-08-02
 */
public class ScreenUtils {
    private static final float ZERO_POINT_FIVE = 0.5f;
    private static final int ONE = -1;

    private ScreenUtils() {
    }

    /**
     * dp转px
     *
     * @param context
     * @param dp
     * @return float
     */
    public static float dp2Px(Context context, float dp) {
        if (context == null) {
            return ONE;
        }
        return dp * density(context);
    }

    /**
     * px转dp
     *
     * @param context
     * @param px
     * @return float
     */
    public static float px2Dp(Context context, float px) {
        if (context == null) {
            return ONE;
        }
        return px / density(context);
    }

    /**
     * 屏幕密度
     *
     * @param context
     * @return float
     */
    public static float density(Context context) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        return display.getAttributes().densityPixels;
    }

    /**
     * dp转int
     *
     * @param context
     * @param vp
     * @return int
     */
    public static int dp2PxInt(Context context, float vp) {
        return (int) (NumberUtil.add(dp2Px(context, vp), ZERO_POINT_FIVE));
    }

    /**
     * px转CeilInt
     *
     * @param context
     * @param px
     * @return float
     */
    public static float px2DpCeilInt(Context context, float px) {
        return (int) (NumberUtil.add(px2Dp(context, px), ZERO_POINT_FIVE));
    }

    /**
     * sp转px
     *
     * @param context
     * @param spValue
     * @return int
     */
    public static int sp2px(Context context, float spValue) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        float fontScale = display.getAttributes().scalDensity;
        return (int) (NumberUtil.add(spValue * fontScale, ZERO_POINT_FIVE));
    }

    /**
     * px转sp
     *
     * @param context
     * @param pxValue
     * @return int
     */
    public static int px2sp(Context context, float pxValue) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        float fontScale = display.getAttributes().scalDensity;
        return (int) (NumberUtil.add(pxValue / fontScale, ZERO_POINT_FIVE));
    }

    /**
     * 显示属性
     *
     * @param context
     * @return DisplayAttributes
     */
    public static DisplayAttributes getDisplayAttributes(Context context) {
        // 获取屏幕密度
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(context);
        DisplayAttributes displayAttributes = display.get().getAttributes();
        return displayAttributes;
    }

    /**
     * 获取屏幕大小
     *
     * @param context
     * @return int[]
     */
    public static int[] getScreenPixelSize(Context context) {
        DisplayAttributes displayAttributes = getDisplayAttributes(context);
        return new int[]{displayAttributes.width, displayAttributes.height};
    }

    /**
     * 屏幕宽
     *
     * @param context
     * @return int
     */
    public static int getScreenWidth(Context context) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        return display.getAttributes().width;
    }

    /**
     * 屏幕高
     *
     * @param context
     * @return int
     */
    public static int getScreenHeight(Context context) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        return display.getAttributes().height;
    }

    /**
     * 状态栏高度
     *
     * @param context
     * @return int
     */
    public static int getStatusBarHeight(Context context) {
        int statusBarHeight = 0;
        try {
            Class<?> mClass = Class.forName("com.android.internal.R$dimen");
            Object obj = mClass.newInstance();
            Field field = mClass.getField("status_bar_height");
            int x1 = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResourceManager().getElement(x1).getInteger();
        } catch (IllegalArgumentException | ClassNotFoundException
                | NoSuchFieldException | IllegalAccessException
                | InstantiationException | IOException
                | NotExistException | WrongTypeException e1) {
            e1.getMessage();
        }
        return statusBarHeight;
    }

    /**
     * 应用内屏幕高度
     *
     * @param context
     * @return int
     */
    public static int getAppInScreenheight(Context context) {
        return getScreenHeight(context) - getStatusBarHeight(context);
    }
}
